﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input.Touch;

class BurstEffect
{
    List<FlyOutParticle> particles;
    public bool Finished { get; set; }
    

    public BurstEffect(List<Texture2D> newTextures, Vector2 minPosition, Vector2 maxPosition, Vector2 minSpeed, Vector2 maxSpeed, float minAccel, float maxAccel,
        float minRotation, float maxRotation, int minTime, int maxTime, int minAmount, int maxAmount, Vector2 minScale, Vector2 maxScale, Random random)
    {        
        particles = new List<FlyOutParticle>();
        int amount = random.Next() % (maxAmount + 1 - minAmount) + minAmount;
        
        for (int i = 0; i < amount; i++)
        {
            int texture = random.Next() % newTextures.Count;
            Vector2 position = new Vector2((float)random.NextDouble() * (maxPosition.X - minPosition.X) + minPosition.X, (float)random.NextDouble() * (maxPosition.Y - minPosition.Y) + minPosition.Y);
            Vector2 speed = new Vector2((float)random.NextDouble() * (maxSpeed.X - minSpeed.X) + minSpeed.X, (float)random.NextDouble() * (maxSpeed.Y - minSpeed.Y) + minSpeed.Y);
            float accel = ((float)random.NextDouble() * (maxAccel - minAccel) + minAccel) * MathHelper.PiOver2;
            Vector2 scale = new Vector2((float)random.NextDouble() * (maxScale.X - minScale.X) + minScale.X, (float)random.NextDouble() * (maxScale.Y - minScale.Y) + minScale.Y);
            float rotation = ((float)random.NextDouble() * (maxRotation - minRotation) + minRotation) * MathHelper.PiOver2;
            int time = random.Next() % (maxTime + 1 - minTime) + minTime;

            particles.Add(new FlyOutParticle(newTextures[texture], position, speed, accel, scale, rotation, time));
        }
    }

    public void Update(GameTimerEventArgs e)
    {        
        for (int i = particles.Count - 1; i >= 0; i--)
        {
            particles[i].Update();
            if (particles[i].Finished)
            {
                particles.RemoveAt(i);
            }
        }
        if (particles.Count <= 0) Finished = true;
    }

    public void Draw(SpriteBatch spriteBatch, Color currentColor)
    {
        foreach (FlyOutParticle particle in particles)
        {
            particle.Draw(spriteBatch, currentColor);
        }
    }
}

class FlyOutParticle
{

    Texture2D texture;
    Vector2 position;
    Vector2 speed;
    Vector2 oldSpeed;
    float acceleration;
    Vector2 scale;
    Vector2 center;
    float rotation;
    float rotationSpeed;
    int maxTime;
    int currentTime;
    public bool Finished { get; set; }

    public FlyOutParticle(Texture2D newTexture, Vector2 newPosition, Vector2 newSpeed, float newAccel, Vector2 newScale, float newRotation, int newMaxTime)
    {
        texture = newTexture;
        position = newPosition;
        speed = newSpeed;
        oldSpeed = speed;
        acceleration = newAccel;
        scale = newScale;
        rotationSpeed = newRotation;
        maxTime = newMaxTime;
        center = new Vector2(texture.Width / 2, texture.Height / 2);
    }

    public void Update()
    {
        if (oldSpeed != Vector2.Zero)
        {
            position += speed;

            if (speed.X > 0)
            {
                speed = new Vector2(speed.X + acceleration, speed.Y);
            }
            else
            {
                speed = new Vector2(speed.X - acceleration, speed.Y);
            }
            if (speed.Y > 0)
            {
                speed = new Vector2(speed.X, speed.Y + acceleration);
            }
            else
            {
                speed = new Vector2(speed.X, speed.Y - acceleration);
            }

            if (oldSpeed.X * speed.X < 0) Finished = true;

        }

        rotation += rotationSpeed;

        currentTime++;
        if (currentTime > maxTime) Finished = true;
    }

    public void Draw(SpriteBatch spriteBatch, Color currentColor)
    {
        float transparent;
        if (oldSpeed.X != 0 && oldSpeed.Y != 0)
        {
            transparent = (speed.X / oldSpeed.X) * (speed.Y / oldSpeed.Y) * (maxTime - currentTime) / (float)maxTime;
        }
        else
        {
            transparent = (maxTime - currentTime) / (float)maxTime;
        }
        spriteBatch.Draw(texture, position, null, currentColor * transparent, rotation, center, scale, SpriteEffects.None, 0);
    }
}
